package com.ruoyi.core.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.core.domain.custombean.*;
import com.ruoyi.core.service.ClientUserService;
import com.ruoyi.core.vo.ClientUserReChargeOperationVO;
import com.ruoyi.core.vo.ClientUserUpdateOperationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * @Description:
 * @Company: 北京卡拉卡尔股份技术有限公司
 * @Author: Darren Ho
 * @Date: 9/29/2020 17:16
 */
@Api("客户端用户信息管理")
@RestController
@RequestMapping("/client-users")
public class ClientUserController extends BaseController {

    @Autowired
    private ClientUserService clientUserService;


    /**
     * 修改用户
     */
    @Log(title = "客户端用户管理", businessType = BusinessType.UPDATE)
    @ApiOperation("编辑用户")
    @PreAuthorize("@ss.hasRole('admin')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ClientUserUpdateOperationVO clientUserUpdateOperationVO) {
        try {
            return AjaxResult.success(clientUserService.update(clientUserUpdateOperationVO));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 修改用户
     */
    @Log(title = "客户端用户管理", businessType = BusinessType.UPDATE)
    @ApiOperation("客户端用户充值")
    @PutMapping("/recharge")
    public AjaxResult recharge(@Validated @RequestBody ClientUserReChargeOperationVO clientUserReChargeOperationVO) {
        try {
            return AjaxResult.success(clientUserService.recharge(clientUserReChargeOperationVO));
        } catch (Exception e) {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, e.getMessage());
        }
    }


    @ApiOperation("用户状态修改")
    @Log(title = "客户端用户管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasRole('admin')")
    @PutMapping("/{id}/start-or-stop/{userStatus}")
    public AjaxResult startOrStop(@PathVariable("id") Long id, @PathVariable("userStatus") UserStatus userStatus) {

        try {
            return toAjax(clientUserService.startOrStop(id, userStatus));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @ApiOperation("用户状态修改")
    @Log(title = "客户端用户管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/removeUser")
    public AjaxResult removeUser(@RequestParam("id") Long id) {

        try {
            return toAjax(clientUserService.removeUser(id));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @ApiOperation("客户端用户明细列表")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping
    public TableDataInfo list(ClientUserQuery clientUserQuery) {
        startPage();
        List<ClientUserListQueryResult> list = clientUserService.findAll(clientUserQuery);
        return getDataTable(list);
    }

    /**
     * 客户端内置用户可操作客户端产品
     */

    @ApiOperation("客户端用户权限明细")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/{clientUserId}/products")
    public AjaxResult getProductsByClientUserId(@PathVariable("clientUserId") Long clientUserId) {
        List<ClientUserProductUsageRightQueryResult> list = clientUserService.findClientProductByClientUserId(clientUserId);
        return AjaxResult.success(list);
    }



    @ApiOperation("客户端当前登录信息明细")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/{clientUserId}/product-users")
    public AjaxResult getOnlineProductUserByClientUserId(@PathVariable("clientUserId") Long clientUserId) {
        List<ProductUserOnlineUsageResult> list = clientUserService.getOnlineProductUserByClientUserId(clientUserId);
        return AjaxResult.success(list);
    }

    @Log(title = "客户端用户管理", businessType = BusinessType.EXPORT)
    @ApiOperation("客户端用户信息导出")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/export")
    public AjaxResult export(@RequestParam(name = "ids", required = false) Long[] ids) {

        List<ClientUserListQueryResult> results;
        if (ids != null && ids.length != 0) {
            results = clientUserService.findByIdSet(ids);
        } else {
            results = clientUserService.findAll(new ClientUserQuery());
        }
        ExcelUtil<ClientUserListQueryResult> util = new ExcelUtil<>(ClientUserListQueryResult.class);
        return util.exportExcel(results, "client_users");
    }


    @Log(title = "客户端用户管理", businessType = BusinessType.DELETE)
    @ApiOperation("客户端用户删除")
    @PreAuthorize("@ss.hasRole('admin')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(clientUserService.deleteByIdSet(ids));
    }

    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/updateCkConfig")
    public AjaxResult updateCkConfig(UpdateCkConfigParams updateCkConfigParams) throws ParseException {
        return AjaxResult.success(clientUserService.updateCkConfig(updateCkConfigParams));
    }

}
